import java.util.*;

/**
 * The node class keeps information of a node and contains the methods 
 * getNeighbors, generateEvent and SendQuery.
 * @author Mattias Hinnerson
 */
public class Node {

	public Position position;
	public HashMap<Integer,Position> eventPathHash = 
											new HashMap<Integer,Position>();
	public HashMap<Integer,Event> events = new HashMap<Integer,Event>();
	public LinkedList<Object> visitorQueue = new LinkedList<Object>();

	public Node(Position pos){

		this.position = pos;
	}
	/**
	 * Gets the neighbors next to the specified position.
	 * @return ArrayList<Position>
	 */
	public ArrayList<Position> getNeighbours(){

		ArrayList<Position> neighborList = new ArrayList<Position>();

		Position returnPos = position.getPosToNorth();
		if(returnPos.getX()!= -1 && returnPos.getY() != -1){
			neighborList.add(returnPos);
		}
		returnPos = position.getPosToNorthWest();
		if((returnPos.getX()>=0 && returnPos.getY() >= 0) && 
				(returnPos.getX()<=49 && returnPos.getY()<=49)){
			neighborList.add(returnPos);
		}
		returnPos = position.getPosToWest();
		if(returnPos.getX()!= -1 && returnPos.getY() != -1){
			neighborList.add(returnPos);
		}
		returnPos = position.getPosToSouthWest();
		if(returnPos.getX()!= -1 && returnPos.getY() != -1){
			neighborList.add(returnPos);
		}
		returnPos = position.getPosToSouth();
		if(returnPos.getX()!= -1 && returnPos.getY() != -1){
			neighborList.add(returnPos);
		}
		returnPos = position.getPosToSouthEast();
		if(returnPos.getX()!= -1 && returnPos.getY() != -1){
			neighborList.add(returnPos);
		}
		returnPos = position.getPosToEast();
		if(returnPos.getX()!= -1 && returnPos.getY() != -1){
			neighborList.add(returnPos);
		}
		returnPos = position.getPosToNorthEast();
		if(returnPos.getX()!= -1 && returnPos.getY() != -1){
			neighborList.add(returnPos);
		}
		return neighborList;
	}
	/**
	 * Generates one event with 0,01% chance.
	 * @param theNetwork
	 */
	public void generateEvent(Network theNetwork){

		Random eventRandom = new Random();
		if(eventRandom.nextInt(10000) == 1){

			Event theEvent = new Event(theNetwork, this);
			events.put(theEvent.id, theEvent);
			theNetwork.addEvent(theEvent);		
		}
	}
	/**
	 * Creates a query and adds it to networks queryList.
	 * @param theNetwork
	 */
	public void sendQuery(Network theNetwork){
		
		Query theQuery = new Query(this, theNetwork);	
		theNetwork.addQuery(theQuery);
	}
}